package com.dingwei.superiordiary.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.MyCollectPostsAdapter;
import com.dingwei.superiordiary.bean.IndexAttentionBean;
import com.dingwei.superiordiary.bean.MyCollectPostsBean;
import com.dingwei.superiordiary.mvp.presenter.MyCollectionPostsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.picktimedialog.PickTimeDialog;
import com.dingwei.superiordiary.picktimedialog.TimePickerView;
import com.dingwei.superiordiary.picktimedialog.listener.IPickTimeDialogListener;
import com.dingwei.superiordiary.picktimedialog.model.VehicleTraceTime;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.TimeUtils;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyCollectPostsActivity extends BaseActivity implements Contract.MyCollectionPostsView {

    MyCollectionPostsPresenter myCollectionPresenter = new MyCollectionPostsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.img_filter)
    ImageView imgFilter;


    private MyCollectPostsAdapter adapter;
    private RecyclerTouchListener onTouchListener;
    private int page = 1;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_collect_posts;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    protected void initView() {
        tvTitle.setText("我的收藏");

        onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
        onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
            @Override
            public void onRowClicked(int position) {
//                        showToast("Row " + (position + 1) + " clicked!");
                MyCollectPostsBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                intent.putExtra("id", bean.id);
                startActivity(intent);
            }

            @Override
            public void onIndependentViewClicked(int independentViewID, int position) {
//                        showToast("Button in row " + (position + 1) + " clicked!");
            }
        })
                .setSwipeOptionViews(R.id.tv_delete)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.tv_delete) {
                            MyCollectPostsBean.DataBean.ListBean bean = adapter.getItem(position);
                            myCollectionPresenter.delete(bean.id, position);
                        }
                    }
                });



        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.bg1), DisplayUtil.dip2px(BaseActivity.context, 8)));
        adapter = new MyCollectPostsAdapter(this);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                myCollectionPresenter.initData(page, "0", "0", false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                myCollectionPresenter.initData(page, "0", "0", true, false);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mRecyclerView.addOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mRecyclerView.removeOnItemTouchListener(onTouchListener);
    }

    public void showData() {
        if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
            noDataLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(MyCollectPostsBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData();
    }

    @Override
    public void onLoadMore(MyCollectPostsBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
    }


    @OnClick({R.id.img_back, R.id.img_filter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_filter:
                final PickTimeDialog pickDilog = new PickTimeDialog(this)
                        .setType(TimePickerView.Type.YEAR_MONTH_DAY)
                        .setOnPickTimeDialogListener(new IPickTimeDialogListener() {
                            @Override
                            public void onSelected(Object object) {
                                VehicleTraceTime vehicleTraceTime = (VehicleTraceTime) object;
                                page = 1;
                                myCollectionPresenter.initData(page, TimeUtils.date2String(vehicleTraceTime.getStartTime().getTime()).split(" ")[0], TimeUtils.date2String(vehicleTraceTime.getStartTime().getTime()).split(" ")[0], true, false);

                            }
                        }).create().cantlable().setOnKeyListener(new PickTimeDialog.onKeyListener() {
                            @Override
                            public void run(DialogInterface dialog, int keyCode, KeyEvent event) {
                                dialog.dismiss();
                            }
                        });
                break;
        }
    }
}
